/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ytmc.videoManager;

import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.swing.table.AbstractTableModel;
import ytmc.Model;

/**
 *
 * @author Tadeusz
 */
public class VideoManagerModel extends AbstractTableModel implements Model
{
    private String[] columnNames = {"Tytuł", "Kategoria", "Opis", "Liczba wizyt"};
    private Class [] columnClasses = {String.class, String.class, String.class, String.class};
    
    private AccountDataRetriever dataRetriever;
    
    public VideoManagerModel()
    {
        dataRetriever = new AccountDataRetriever();
        dataRetriever.getAccountData();
    }
    
    public void refresh() throws InterruptedException, InvocationTargetException 
    {
        dataRetriever.getVideos().clear();
        dataRetriever.getAccountData();
        fireTableDataChanged();
    }
    
    
    public void remove(int row) throws IOException, ServiceException
    {
        if(!(dataRetriever.getVideos().isEmpty()))
        {
            dataRetriever.removeVideo(dataRetriever, row);
            dataRetriever.getVideos().remove(row);
            fireTableRowsDeleted(row, row);
        }
        
    }
    
    public AccountDataRetriever getDataRetriever()
    {
        return dataRetriever;
    }
     
    
    @Override
    public Class<?> getColumnClass(int i)
    {
        return columnClasses[i];
    }

    @Override
    public String getColumnName(int i)
    {
        return columnNames[i];
    }

    @Override
    public int getRowCount() 
    {
        return dataRetriever.getVideos().size();
        
    }

    @Override
    public int getColumnCount() 
    {
        return columnNames.length;
        
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) 
    {
        if( rowIndex < 0 || rowIndex >= getRowCount() )
        {
            return null;
        }
        
        switch(columnIndex)
        {
            case 0:
                return dataRetriever.getVideos().get(rowIndex).getTitle();
            case 1:
                return dataRetriever.getVideos().get(rowIndex).getCategory();
            case 2:
                return dataRetriever.getVideos().get(rowIndex).getDescription();
            case 3:
                return dataRetriever.getVideos().get(rowIndex).getVisitNumber();
            default:
		return null;
                
        }
       
    }
    


}
